/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

import com.aphrodite.image.ColorUtil;

//not tested

/**
 * Fading text effect
 */
public class FadingText extends TextEffect {
	// private class Ani extends TimerTask {
	// public void run() {
	// while (isFading) {
	// doFade();
	// reDrawWidget();
	// try {
	// Thread.sleep(fadingInterval);
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }
	//
	// }
	//
	// }

	int cStep;
	private int currentColor;
	public int delay = 0; // time till the effect starts
	private int endColor = 0xFF80FF00;
	// private int fadingInterval = 200;
	private int[] gradient;
	private final short in = 10;
	// private boolean isFading;

	private final short out = 20;

	private int startColor = 0xFF0080FF;

	private int stepsIn = 5, stepsOut = 5; // fading duration

	public int sWaitTimeIn = 10; // time to stay faded in

	public int sWaitTimeOut = 0; // time to stay faded out

	// private String text;
	// private Timer timer;
	private short type = in;

	public FadingText(Theme theme, String text) {
		super(theme, 0, 0);
		this.text = text;
		updateModel();
	}

	// public void cleanUp() {
	//
	// }

	// private void doFade() {
	//
	// }

	protected void doAniTask() {
		cStep++;
		if (this.cStep < this.stepsIn) {
			// fade in
			this.currentColor = this.gradient[this.cStep];

		} else if (this.cStep < this.stepsIn + this.sWaitTimeIn) {
			// have a break
			if (this.currentColor != this.endColor) {
				this.currentColor = this.endColor;

			}
		} else if (this.cStep < this.stepsIn + this.sWaitTimeIn + this.stepsOut) {
			// fade out
			this.currentColor = this.gradient[this.stepsIn + this.sWaitTimeIn + this.stepsOut - this.cStep - 1];

		} else {
			// have another break
			if (this.currentColor != this.startColor) {
				this.currentColor = this.startColor;
			}
			// isFading = false;
			doEffect = false;
		}
	}

	// public void setFadingInterval(int millisec) {
	// this.fadingInterval = millisec;
	// stateChanged(this);
	// }

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		// draw the current state
		g.setFont(theme.widgetFont);
		g.setColor(currentColor);
		g.drawString(text, 2, 2, 0);
		release(g, width, height, false);

	}

	// public void startFading() {
	// // if (!isFading) {
	// // isFading = true;
	// // timer = new Timer();
	// // timer.schedule(new Ani(), 0);
	// // }
	// super.start();
	// }

	// public void stopFading() {
	// // if (isFading) {
	// // isFading = false;
	// // timer.cancel();
	// // }
	// super.stop();
	// }

	private void setup() {
		this.cStep = 0;
		switch (type) {
		case out:
			this.stepsIn = 0;
			this.sWaitTimeIn = 0;
			this.currentColor = this.endColor;
			break;
		case in:
			this.stepsOut = 0;
			this.sWaitTimeOut = 0;
			this.currentColor = this.startColor;
			break;
		default:// loop
			this.currentColor = this.startColor;
		}

		this.cStep -= this.delay;
		gradient = ColorUtil.getGradient(startColor, endColor, Math.max(this.stepsIn, this.stepsOut));
	}

	protected void updateModel() {
		setup();
		this.width = theme.widgetFont.stringWidth(text) + 4;
		this.height = theme.widgetFont.getHeight() + 4;

	}

}
